package com.at.bilibili.api;

import com.at.bilibili.api.support.UserSupport;
import com.at.bilibili.domain.JsonResponse;
import com.at.bilibili.domain.UserMoment;
import com.at.bilibili.domain.annotation.ApiLimitedRole;
import com.at.bilibili.domain.annotation.DataLimited;
import com.at.bilibili.domain.constant.AuthRoleConstant;
import com.at.bilibili.service.UserMomentsService;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserMomentApi {
    @Autowired
    private UserMomentsService userMomentsService;
    @Autowired
    private UserSupport userSupport;

    /**
     * 用户发布动态
     */
    // 4 对应 Lv0 也就是等级为0的用户不能发布动态
    @ApiLimitedRole(limitedRoleCodeList = {AuthRoleConstant.ROLE_LV0})
    // 进行传入数据的校验 lv1 只能发布 type为0的动态
    @DataLimited
    @PostMapping("/user-moments")
    public JsonResponse<String> addUserMoments(@RequestBody UserMoment userMoment) throws RemotingException, InterruptedException, MQClientException, MQBrokerException {
        Long userId = userSupport.getCurrentUserId();
        userMoment.setUserId(userId);
        userMomentsService.addUserMoments(userMoment);
        return JsonResponse.success();
    }
    /**
     * 查询所订阅用户的动态
     */
    @GetMapping("/user-subscribed-moments")
    public JsonResponse<List<UserMoment>> getUserSubscribedMoments(){
        Long userId = userSupport.getCurrentUserId();
        List<UserMoment> list = userMomentsService.getUserSubscribedMoments(userId);
        return new JsonResponse<>(list);
    }

}
